import requests
from urllib.parse import quote


def getNetInformation():
    res = requests.get("http://2.2.2.2/")
    cookies = res.cookies
    res = res.text
    res = res.replace("<html>", "").replace("</html>", "") \
        .replace("<head>", "").replace("</head>", "") \
        .replace("<script>", "").replace("</script>", "") \
        .replace("<body>", "").replace("</body>", "")
    res = res.split("\"")[1].replace("&url=", "")
    res = res.replace("https://pt.gcac.edu.cn/portal.do?", "").split("&")
    if len(res) < 3:
        raise ZeroDivisionError("获取信息失败，检查是否连上校园网，或已登录")
    return res, cookies


def login(host, router, header):
    url = host + router
    res = requests.get(url=url, headers=header)
    return res.json()


def addParameter(path, data):
    for i in data:
        path += "&" + i
    return path


def addParameterByDict(path, data):
    for i in data:
        path += "&" + i + "=" + data[i]
    return path


def urlDecode(data, format):
    return quote(data, format)


def loginNet(userid, passwd, header):
    path = '/quickauth.do'
    user = {'userid': '',
            'passwd': '',
            'wlanacIp': '10.95.254.1',
            'ssid': '',
            'bindCtrlId': '',
            'version': '0',
            'portalpageid': '2',
            'uuid': '',
            'portaltype': '',
            'timestamp': '',
            }
    user['userid'] = userid
    user['passwd'] = passwd
    user['passwd'] = urlDecode(user['passwd'], 'utf-8')
    netInformation, cookies = getNetInformation()
    # print(netInformation)
    if len(netInformation) < 3:
        raise ZeroDivisionError("获取信息失败，检查是否连上校园网，或已登录")
    netInformation[2] = "mac=" + urlDecode(netInformation[2].replace("mac=", ""), 'utf-8')
    router = addParameter(path, netInformation)
    router = addParameterByDict(router, user)

    header['GET'] = router


    router='/quickauth.do?userid=&passwd=&wlanuserip=&wlanacname=&wlanacIp=10.95.254.1&ssid=&vlan=&mac=&version=0&portalpageid=2&timestamp=&uuid=&portaltype=&hostname=&bindCtrlId='
    userid = 'userid=' + user['userid']
    router = router.replace("userid=",userid)
    passwd = 'passwd=' + user['passwd']
    router = router.replace("passwd=",passwd)

    # print(netInformation)

    router = router.replace('wlanuserip=',netInformation[0])
    router = router.replace('wlanacname=',netInformation[1])
    router = router.replace('mac=',netInformation[2])
    router = router.replace('vlan=',netInformation[3])
    router = router.replace("hostname=", netInformation[4])

    print('http://pt.gcac.edu.cn' + router)
    res = login(host='http://pt.gcac.edu.cn',router=router,header=header)
    return res

def disConnect(acip, userip, acname, mac, userid, header):
    date = {
        'wlanacip':acip,
        'wlanuserip':userip,
        'wlanacname':acname,
        'version':'0',
        'portaltype':'',
        'userid':userid,
        'mac':mac,
        'groupid':''
    }
    res = requests.post('https://pt.gcac.edu.cn/quickauthdisconn.do',headers=header,data=date)
    return res.json()